package test;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

import org.andengine.engine.Engine;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;


import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.input.touch.TouchEvent;

import android.util.Log;
import android.view.Display;


public class JuegoMago extends SimpleBaseGameActivity {

	/**
	 * Constantes
	 */
	private static int CAMERA_WIDTH = 1080;
	private static int CAMERA_HEIGHT = 720;


	/**
	 * Atributos
	 */
	private Camera mCamera;
	private Scene mMainScene;


	// MAgo
	private BitmapTextureAtlas mBitmapTextureAtlas;
//	private TextureRegion mPlayerTextureRegion;
//	private Sprite player;

	// Los Piratas
	private TextureRegion mTargetTextureRegion;
	private LinkedList targetLL;
	private LinkedList TargetsToBeAdded;

	//Los Fireballs
//	private LinkedList projectileLL;
//	private LinkedList projectilesToBeAdded;
//	private TextureRegion mProjectileTextureRegion;

	//	@Override
	//	public EngineOptions onCreateEngineOptions() {
	////		final Display display = getWindowManager().getDefaultDisplay();
	//	 	 
	//	    mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
	////	    mEngine.registerUpdateHandler(new FPSLogger());
	//	    
	//	   return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
	////	    return null;
	//	}
	//
	//	@Override
	//	public void onCreateResources(
	//			OnCreateResourcesCallback pOnCreateResourcesCallback)
	//					throws IOException {
	//		
	//			mBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 250, 250, TextureOptions.DEFAULT);
	//	
	//		    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");
	//		 
	//		    mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
	//		        .createFromAsset(this.mBitmapTextureAtlas, this, "Fireball.png",0, 0);
	//		    mBitmapTextureAtlas.load();
	////		    mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);
	//
	//	}
	//
	//	@Override
	//	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
	//			throws IOException {
	//		
	//		final int PlayerX = (int) (this.mPlayerTextureRegion.getWidth() / 2);
	//		final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
	//		    .getHeight()) / 2);
	//		player = new Sprite(PlayerX, PlayerY, mPlayerTextureRegion, getVertexBufferObjectManager());
	//	    mMainScene = new Scene();
	//	    mMainScene.attachChild(player);
	//	}


	@Override
	protected void onCreateResources() throws IOException {
		mBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 650, 650, TextureOptions.DEFAULT);

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");

//		mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
//				.createFromAsset(this.mBitmapTextureAtlas, this, "Wizard.png",0, 0);
		mBitmapTextureAtlas.load();

		mTargetTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "Target.png",
				100, 0);

		//		128

//		mProjectileTextureRegion = BitmapTextureAtlasTextureRegionFactory
//				.createFromAsset(this.mBitmapTextureAtlas, this,"Fireball.png", 200, 0);

		//		64

			    mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);

	}

	@Override
	protected Scene onCreateScene() {

//		final int PlayerX = (int) (this.mPlayerTextureRegion.getWidth() / 2);
//		final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
//				.getHeight()) / 2);
//		player = new Sprite(PlayerX, PlayerY, mPlayerTextureRegion, getVertexBufferObjectManager());

		mMainScene = new Scene();
//		mMainScene.attachChild(player);
//
//		//Listener
//		mMainScene.setOnSceneTouchListener(this);

		// Piratas
		targetLL = new LinkedList();
		TargetsToBeAdded = new LinkedList();

//		//Crear nuevos piratas
//		createSpriteSpawnTimeHandler();
//		mMainScene.registerUpdateHandler(detect);
		
		// Los fireballs
//		projectileLL = new LinkedList();
//		projectilesToBeAdded = new LinkedList();
		createSpriteSpawnTimeHandler();

		return mMainScene;

	}

	@Override
	public EngineOptions onCreateEngineOptions() {

		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
	}


//	@Override
//	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
//		boolean retorno = false;
//		if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
//			final float touchX = pSceneTouchEvent.getX();	
//			final float touchY = pSceneTouchEvent.getY();
//			//			shootProjectile(touchX, touchY);
//			//			shootProjectile(player.getX(), player.getY());
//			Log.i("posiciones tap", "pos X: "+touchX + " pos Y: " +touchY );
////			castFireball(touchX, touchY);
//			return !retorno;
//		}
//		//			else {
//		//			if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE) {
//		//				
//		//			}
//		//		}
//		return retorno;
//	}




//	private void castFireball(float x, float y) {
//
//
//		Sprite spriteFireball = new Sprite(x, y, mProjectileTextureRegion.deepCopy(), getVertexBufferObjectManager());
//		mMainScene.attachChild(spriteFireball);
//
//		int actualDuration = 2;
//
//		MoveXModifier deltaX = new MoveXModifier(actualDuration, spriteFireball.getX(), -spriteFireball.getWidth());
//
//		spriteFireball.registerEntityModifier(deltaX.deepCopy());
//		TargetsToBeAdded.add(spriteFireball);
//
//	}

	// Metodos

	public void addTarget() {
		Random rand = new Random();

		int x = (int) ((int) mCamera.getWidth() + mTargetTextureRegion.getWidth());
		int minY = (int) mTargetTextureRegion.getHeight();
		int maxY = (int) (mCamera.getHeight() - mTargetTextureRegion
				.getHeight());
		int rangeY = maxY - minY;
		int y = rand.nextInt(rangeY) + minY;

		Sprite target = new Sprite(x, y, mTargetTextureRegion.deepCopy(), getVertexBufferObjectManager());
		mMainScene.attachChild(target);

		int minDuration = 2;
		int maxDuration = 4;
		int rangeDuration = maxDuration - minDuration;
		int actualDuration = rand.nextInt(rangeDuration) + minDuration;

		MoveXModifier mod = new MoveXModifier(actualDuration, target.getX(),
				-target.getWidth());
		target.registerEntityModifier(mod.deepCopy());

		TargetsToBeAdded.add(target);
	}


	private void createSpriteSpawnTimeHandler() {
		TimerHandler spriteTimerHandler;
		float mEffectSpawnDelay = 1f;

		ITimerCallback hola =new ITimerCallback() {

			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				addTarget();

			}
		};

		spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true,hola);
		getEngine().registerUpdateHandler(spriteTimerHandler);

	}

//	public void removeSprite(final Sprite _sprite, Iterator it) {
//		runOnUpdateThread(new Runnable() {
//
//			@Override
//			public void run() {
//				mMainScene.detachChild(_sprite);
//			}
//		});
//		it.remove();
//	}
//
//	IUpdateHandler detect = new IUpdateHandler() {
//		@Override
//		public void reset() {
//		}
//
//		@Override
//		public void onUpdate(float pSecondsElapsed) {
//
//			Iterator<Sprite> targets = targetLL.iterator();
//			Sprite _target;
//
//			while (targets.hasNext()) {
//				_target = targets.next();
//				if (_target.getX() <= -_target.getWidth()) {
//					removeSprite(_target, targets);
//				}
//			}
//			targetLL.addAll(TargetsToBeAdded);
//			TargetsToBeAdded.clear();
//		}
//	};

	//	private void shootProjectile(final float pX, final float pY) {
		//
		//		int offX = (int) (pX - player.getX());
	//		int offY = (int) (pY - player.getY());
	////		if (offX <= 0)
	////			return;
	//
	//		final Sprite projectile;
	//		projectile = new Sprite(player.getX(), player.getY(), mProjectileTextureRegion.deepCopy(), getVertexBufferObjectManager());
	//		mMainScene.attachChild(projectile);
	//
	//		int realY = (int) (mCamera.getWidth() + projectile.getWidth() / 2.0f);
	//		float ratio = (float) offY / (float) offX;
	//		int realX = (int) ((realY * ratio) + projectile.getX());
	//
	//		int offRealX = (int) (realX - projectile.getX());
	//		int offRealY = (int) (realY - projectile.getY());
	//		float length = (float) Math.sqrt((offRealX * offRealX)
	//				+ (offRealY * offRealY));
	//		float velocity = 300.0f / 1.0f; // 480 pixels / 1 sec
	//		float realMoveDuration = length / velocity;
	//
	//		MoveModifier mod = new MoveModifier(realMoveDuration,
	//				projectile.getX(), realX, projectile.getY(), realY);
	//		projectile.registerEntityModifier(mod.deepCopy());
	//
	//		projectilesToBeAdded.add(projectile);
	//	}



}
